package GUI; //done

import java.awt.Font;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client.ConnectionToServer;
import Entities.DealSale;
import Entities.Template;

/**
 * This Class controls the Template Search Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamel
 */
public class TemplateSearchWindow extends JFrame{
	
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;
	
	/** The Template ID ComboBox. */
	private JComboBox tempIDComboBox = null;
	
	/** The Template ID Label. */
	private JLabel tempIDlLabel = null;
	
	/** The Search Button. */
	private JButton searchButton = null;
	
	/** The Content Pane. */
	private JPanel jContentPane = null;
	
	/** The Template Window. */
	private TemplateWindow templateWindow = null;
	
	/** The Template. */
	private Template template = null;
	
	/** The DealSale Window. */
	private DealSaleWindow dealsaleForm = null;
	
	/** The Template ID. */
	private long tempID = 0;
	
	/** The Template Name. */
	private String tname = null;
	
	/** The Initialize Window. */
	private String initializeWindow;
	
	/** The DealSale. */
	private DealSale dealsale = null;
	
	/** The Schedule Window. */
	private ScheduleWindow scheduleWindow = null;
	
	/** The Background Label. */
	private JLabel BackgroundLabel;
	
	/**
	 * Default constructor
	 */
	public TemplateSearchWindow(String initializeWindow){
		super();
		setIconImage(Toolkit.getDefaultToolkit().getImage(TemplateSearchWindow.class.getResource("/javax/swing/plaf/metal/icons/ocean/collapsed-rtl.gif")));
		this.initializeWindow = initializeWindow;
		initialize();		
	}
	
	/**
	 * This function initializes TemplateSearchWindow
	 */
	private void initialize() {
		template = new Template(0, "", null, "", "");
		dealsale = new DealSale(0,null, null, null, template);
		dealsaleForm = new DealSaleWindow();
		scheduleWindow = new ScheduleWindow(dealsale);
		this.setContentPane(getJContentPane());
		this.setTitle("Template Search");
		BackgroundLabel = new JLabel("");
	    BackgroundLabel.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/bg.png")));
	    BackgroundLabel.setBounds(0, 0, 600, 480);
		getContentPane().add(BackgroundLabel);
	}
	
	/**
	 * This function initializes JContentPane
	 */
	private JPanel getJContentPane(){
		if (jContentPane == null){
			jContentPane = new JPanel();
			jContentPane.setBackground(SystemColor.activeCaption);
			jContentPane.setForeground(Color.BLACK);
			jContentPane.setLayout(null);
			
			tempIDlLabel = new JLabel();
			tempIDlLabel.setBackground(Color.RED);
			tempIDlLabel.setBounds(new Rectangle(85, 73, 113, 22));
			tempIDlLabel.setText("Template number:");
							
			jContentPane.setLayout(null);
			jContentPane.add(tempIDlLabel, null);
			jContentPane.add(getTempIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);
			templateWindow = new TemplateWindow();
			
			templateWindow.getBackjButton().addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setContentPane(jContentPane);								
				}
			});
			
			templateWindow.getBackjButton().addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setContentPane(jContentPane);								
				}
			});
			
			dealsaleForm.getNextjButton().addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (dealsaleForm.ifNext()){
						scheduleWindow.setDealSale(dealsaleForm.getDealSale());
						scheduleWindow.setProdID(template.getProduct().getProdID());
						scheduleWindow.updateCustomersjComboBox();
						scheduleWindow.updateSalesMenjComboBox();
						
						JLabel backgroundLabel = new JLabel("");
						backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
						backgroundLabel.setBounds(0, 0, 550, 420);
						scheduleWindow.add(backgroundLabel);
						
						setSize(550, 420);
						setContentPane(scheduleWindow);
					}	
				}	
			});
		}
		return jContentPane;		
	}
	
	/**
	 * This function initializes getTempIDComboBox	
	 */
	private JComboBox getTempIDComboBox(){
		if(tempIDComboBox == null){	
			tempIDComboBox = new JComboBox();
			tempIDComboBox.setForeground(Color.BLACK);
			tempIDComboBox.setBounds(new Rectangle(234, 75, 127, 25));
			String[] templates = null;
			try{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				templates = (String[]) connectiontoserver.loadAllTemplates();
				if (templates == null){
					JOptionPane.showMessageDialog(null, "Templates is EMPTY!!!");
					tempIDComboBox.addItem(" ");
				}	
				else{
					int len = templates.length;
					if (len > 0)
						tname = templates[0];
					if(len == 0)
						tempIDComboBox.addItem(0);
					else
						tempIDComboBox.addItem(" ");
					for(int i=0; i < len; i++){
						tempIDComboBox.addItem(templates[i]);
					}	
				}
			}
			catch(Exception e){
				tempIDComboBox.addItem(" ");
			}
			tempIDComboBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e){
						tname = (String)tempIDComboBox.getSelectedItem();	
						if (tname.equals(":"))
							return;
						try{
							tempID = Integer.parseInt(tname.substring(0, tname.indexOf(":")));
						}
						catch( NumberFormatException exc){
							return;
						}	
				}	
			});	
		}	
		return tempIDComboBox;
	}

	
	/**
	 * This function initializes getSearchButton	
	 */
	private JButton getSearchButton() {
		if (searchButton == null) {
			searchButton = new JButton();
			searchButton.setForeground(Color.BLACK);
			searchButton.setBackground(SystemColor.scrollbar);
			searchButton.setFont(new Font("David", Font.BOLD, 14));
			searchButton.setBounds(new Rectangle(154, 136, 130, 29));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e){
					switch(initializeWindow){
					case "UpdateTemplate":
						initialiToUpdateTemplateForm();
						return;
						
					case "DealSale":
						initialiToDealSaleForm();
						return;
					}	
				}	
			});
		}
		return searchButton;
	}
	
	/**
	 * This function initializes initialiToUpdateTemplateForm
	 */
	private void initialiToUpdateTemplateForm(){		
		try{
			if (tempID > 0){
				if (template.loadTemplate(tempID)){
					templateWindow.loadTemplate(template);
					templateWindow.addUpdateButton();
					this.setTitle("Template update");
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
					backgroundLabel.setBounds(0, 0, 520, 330);
					templateWindow.add(backgroundLabel);
					
					setContentPane(templateWindow);
				}	
				else
					JOptionPane.showMessageDialog(null, "Template does not exists in DB!","",JOptionPane.WARNING_MESSAGE);
			}
			else
				JOptionPane.showMessageDialog(null, "Please select a Template!");
		} 
		catch (Exception excp){
			JOptionPane.showMessageDialog(null, "Error!","",JOptionPane.ERROR_MESSAGE);	
		}											
	}
	
	/**
	 * This function initializes initialiToDealSaleForm
	 */
	private void initialiToDealSaleForm(){		
		try {
			if (tempID > 0){
				if (template.loadTemplate(tempID)){
					dealsale.setTemplate(template);
					dealsaleForm.loadDealSale(dealsale);
					this.setTitle("Deal Sale");
					dealsaleForm.addNextjButton();
					
					JLabel backgroundLabel = new JLabel("");
					backgroundLabel.setIcon(new ImageIcon(AddCustomerInterestWindow.class.getResource("/Pictures/bg.png")));
					backgroundLabel.setBounds(0, 0, 520, 500);
					dealsaleForm.add(backgroundLabel);
					
					setContentPane(dealsaleForm);
				}	
				else
					JOptionPane.showMessageDialog(null, "Template does not exists in DB!","",JOptionPane.WARNING_MESSAGE);
			}
			else
				JOptionPane.showMessageDialog(null, "Please select a Template!");
		} 
		catch (Exception excp){
			JOptionPane.showMessageDialog(null, "Error!","",JOptionPane.ERROR_MESSAGE);	
		}											
	}
}
